{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5120ef0f-8510-4ad7-a0ea-2e306aec0064",
   "metadata": {
    "tags": []
   },
   "source": [
    "## Adding Bash Dependencies to an Electron\n",
    "\n",
    "Bash dependencies can be passed to an electron as an argument to the electron decorator. The dependencies can take one of two forms:\n",
    "\n",
    "1. A Covalent DepsBash object.\n",
    "2. A list of commands represented as strings.\n",
    "\n",
    "Both forms are illustrated below.\n",
    "\n",
    "### Prerequisites\n",
    "\n",
    "Import `covalent`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b6ffa4e9-73c0-46b9-be97-104a7f44cd97",
   "metadata": {},
   "outputs": [],
   "source": [
    "import covalent as ct "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5bcbcd0-646c-4b96-85d2-ef30b30ac085",
   "metadata": {},
   "source": [
    "### Procedure\n",
    "\n",
    "#### Using a Covalent DepsBash object\n",
    "\n",
    "1. Pass a comma-separated list of Bash commands to the constructor of a `DepsBash()` object, then assign the object to the `deps_bash` keyword argument:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a30852d1-2fa4-4ffb-b01d-7e7cc9925182",
   "metadata": {},
   "outputs": [],
   "source": [
    "@ct.electron(\n",
    "        deps_bash=ct.DepsBash([\"echo `whereis cpp` > /tmp/path.txt\"]),\n",
    ")\n",
    "def get_bash_result():\n",
    "    with open('/tmp/path.txt','r') as f:\n",
    "        return f.read()     "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79b3012b-ecf8-4c96-a0c7-a3b11d69a10e",
   "metadata": {},
   "source": [
    "2. Use the electron in a lattice and dispatch the lattice as usual:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9384c47b",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cpp: /usr/bin/cpp\n",
      "\n"
     ]
    }
   ],
   "source": [
    "@ct.lattice\n",
    "def workflow():\n",
    "    return get_bash_result()\n",
    "\n",
    "dispatch_id = ct.dispatch(workflow)()\n",
    "r = ct.get_result(dispatch_id, wait=True)\n",
    "print(r.result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "371700ea",
   "metadata": {},
   "source": [
    "#### Using a List\n",
    "\n",
    "1. Pass a list of commands – represented as strings – directly to the `deps_bash` named argument:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "36461b39-9518-41ab-9ce5-897835d5f35a",
   "metadata": {},
   "outputs": [],
   "source": [
    "@ct.electron(\n",
    "        deps_bash=[\"echo `whereis cpp` /tmp/path.txt\"],\n",
    ")\n",
    "def get_bash_result():\n",
    "    with open('/tmp/path.txt','r') as f:\n",
    "        return f.read()   "
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "528fc0c1",
   "metadata": {},
   "source": [
    "2. Dispatch the electron in a lattice as usual:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "28cc761e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cpp: /usr/bin/cpp\n",
      "\n"
     ]
    }
   ],
   "source": [
    "@ct.lattice\n",
    "def workflow():\n",
    "    return get_bash_result()\n",
    "\n",
    "dispatch_id = ct.dispatch(workflow)()\n",
    "r = ct.get_result(dispatch_id, wait=True)\n",
    "print(r.result)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "3dd5350d",
   "metadata": {},
   "source": [
    "### See Also\n",
    "\n",
    "[Constructing Tasks from Bash Scripts](./construct_bash_task.ipynb)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "vscode": {
   "interpreter": {
    "hash": ""
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
